/**
 * Created with JetBrains PhpStorm.
 * User: power_yin
 * Date: 13-9-27
 * Time: 上午1:26
 * To change this template use File | Settings | File Templates.
 */
/**
 * select-tab 组件切换
 * @param Hash Object setting
 * setting = {
 *  "wrapper_jQ" : 'a jQuery Selector'
 * }
 */
(function(xf){
    "use strict";
    xf.SelectTab = (function(){
         function SelectTab(setting){
            this.wrapper_jQ = setting.wrapper_jQ;
            this.activeEventName = setting.activeEventName;
            this.contentPanels = null;
            this.tabBtnGroup = null;
            this.Node2Stack = new XF.Node2Stack();
            this._init();   //初始化
         }
        /**
         * 切换方法 switchTab
         * @param targetComponents 所有 SelectTab 定义的组件
         */
         SelectTab.prototype.switchTab = function(targetComponents){
             var components = {}; //定义 Node2Stack 需要的 组件集合
             // 初始化 选项卡内容 组件
             var contentPanelId = targetComponents.contentPanel_jQ.attr('id');
             components[ 'contentPanel' ] = new XF.SelectTab.ContentPanel({
                 "instance_jQ": targetComponents.contentPanel_jQ
             });
             // 初始化 按钮 组件
             var tabBtnId = targetComponents.tabBtn_jQ.data('target-id');
             components[ 'tabBtn' ] = new XF.SelectTab.TabBtn({
                 "instance_jQ": targetComponents.tabBtn_jQ
             });
             // 将两个组件 推入 Node2Stack
             this.Node2Stack.push(components);
             //初始化 时 激活 以下组件
             this.Node2Stack.current['contentPanel'].active();
             this.Node2Stack.current['tabBtn'].active();
         };
        /**
         * 初始化方法
         * @private
         */
         SelectTab.prototype._init = function(){
            /**
             * 初始化事宜主要包括
             * 1，获取所有按钮 和内容面板
             * 2，注册所有按钮事件
             * 3，执行第一次切换 （第一个）
             * */
             this.contentPanels = this.wrapper_jQ.find('.select-content-panel>.item-content-panel');
             this.tabBtnGroup = this.wrapper_jQ.find('.select-btn-group');
             this._itemClick();
             this.switchTab({
                 "contentPanel_jQ" : this.contentPanels.first(),
                 "tabBtn_jQ" : this.tabBtnGroup.find('.select-btn').first()
             });
          };
        /**
         * 按钮点击事件 具体实现
         * @private
         */
         SelectTab.prototype._itemClick = function(){
             var $this = this;
             this.tabBtnGroup.delegate('.select-btn', this.activeEventName, {}, function(event){
                 var targetBtn_jQ = $(this),
                     targetBtnId = targetBtn_jQ.data('target-id');
                 $this.contentPanels.each(function(index, targetPanel){
                    var targetContentPanel_jQ = jQuery(targetPanel),
                        targetSelectContentPanelId = targetContentPanel_jQ.attr('id');
                    if( targetBtnId ==  targetSelectContentPanelId){
                        // 切换面板
                        $this.switchTab({
                            "contentPanel_jQ" : targetContentPanel_jQ,
                            "tabBtn_jQ" : targetBtn_jQ
                        });
                    }
                 });
             });

         };
         return SelectTab;
    })();
    /**
     * SelectPanel -- SelectTab 的组件部分
     * @param setting Hash Object
     * setting = {
     *     "instance_jQ" : SelectPanel jQuery Instance
     * }
     */
    xf.SelectTab.ContentPanel = (function(){
        function ContentPanel(setting){
            this.instance_jQ = setting.instance_jQ;
        }
        ContentPanel.prototype.resume = function(){
            this.instance_jQ.removeClass('active');
        };
        ContentPanel.prototype.active = function(){
            this.instance_jQ.addClass('active');
        };
        return ContentPanel;
    })();
    /**
     * SelectBtn -- SelectTab 的组件部分
     * @param setting Hash Object
     * setting = {
     *     "instance_jQ" : SelectBtn jQuery Instance
     * }
     */
    xf.SelectTab.TabBtn = (function(){
        function TabBtn(setting){
            this.instance_jQ = setting.instance_jQ;
        }
        TabBtn.prototype.resume = function(){
            this.instance_jQ.removeClass('active');
        };
        TabBtn.prototype.active = function(){
            this.instance_jQ.addClass('active');
        };
        return TabBtn;
    })();
})(XF || ( XF = {} ));